package com.study.example.gameserver.module.scene.bt.node;

import com.sparrow.common.ai.behaviortree.BehaviorContext;
import com.sparrow.common.ai.behaviortree.node.impl.ConditionNode;
import com.sparrow.common.aoi.util.MapUtil;
import com.study.example.gameserver.module.scene.domain.SceneMap;
import com.study.example.gameserver.module.scene.entity.IPlayerEntity;
import com.study.example.gameserver.module.scene.entity.impl.Monster;

/**
 * 是否敌人在攻击范围
 */
public class IsEnemyInAttackRange extends ConditionNode {
    public static final int ATTACK_RANGE = 4;
    @Override
    public boolean check(BehaviorContext context) {
        Monster monster = context.getOwner();
        if (monster == null) {
            return false;
        }
        SceneMap sceneMap = context.get("SceneMap", SceneMap.class);
        if (sceneMap == null) {
            return false;
        }
        if (monster.getTargetId() == 0) {
            return false;
        }
        IPlayerEntity target = sceneMap.getPlayers().get(monster.getTargetId());
        if (target == null) {
            return false;
        }
        if (!target.isAlive()) {
            return false;
        }
        float distance = MapUtil.getDistance(monster.getPos(), target.getPos());
        if (distance > ATTACK_RANGE) {
            return false;
        }
        return true;
    }

}
